/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.reflector.ui;

import java.io.IOException;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
/**
 * Timer Component for timing how fast code executed between time tag
 *
 * @author devil
 *
 */
public class UITimer extends UIComponentBase {
	public UITimer() {
		super();
	}

	private Long startTime;
	
	//Passed properties
	private String name;	

	//Current state
	private Object[] state;

	public String getName() {
		if (name != null) {
			return name;
		}
		ValueExpression exp = this.getValueExpression("name");
		if (exp != null) {
			try {
				return (String)exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return "Time";
	}



	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		assertValidInput(context);
		if (!isRendered()) {
			return;
		}
		ResponseWriter writer=context.getResponseWriter();
		writer.write("<fieldset class='jsfdump-timer'>");
		startTime=System.currentTimeMillis();
	}

	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		assertValidInput(context);
		if (!isRendered()) {
			return;
		}
		super.encodeEnd(context);
		ResponseWriter writer=context.getResponseWriter();
		long timerDelta=System.currentTimeMillis()-startTime;
		
		writer.write("<legend>"+getName()+" : "+ timerDelta +" ms");		
		writer.write("</legend></fieldset>");
		//String script=" ";		
		//String id=this.getClientId(context);
	}
	
	@Override
	public String getFamily() {
		return null;
	}


	@Override
	public void restoreState(FacesContext context, Object state) {
		this.state=(Object[])state;

	}
	@Override
	public Object saveState(FacesContext context) {
		if(state==null){
			state=new Object[1];			
		}
		this.state[0] = super.saveState(context);
		return this.state;
	}


	/**
	 * This method checks for null context and component
	 * 
	 * @param context
	 * @param component
	 */
	private void assertValidInput(FacesContext context) {
		if (context == null) {
			throw new NullPointerException();
		} 
	}
}
